//
//  HWGameLiveChatBaseView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/10/26.
//

import UIKit
import JXSegmentedView
class HWGameLiveChatBaseView: UIView {
    var topBgView:UIView!
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var ownerBgView:UIView!
    var ownerHeadImageView:UIImageView!
    var ownerName:UILabel!
    var heartImageView:UIImageView!
    var zanLabel:UILabel!
    
    var followButton:UIButton!
    /// 聊天
    var chatButtonClick:(()->())?
    /// 管理
    var managerButtonClick:(()->())?
    /// 送礼物
    var giftButtonClick:(()->())?
    ///跳转游戏
    var downLoadButtonClick:((_:HWVoiceRoomGameModel)->())?
    var followButtonClick:(()->())?
    let chatView = HWGameLiveChatView()
    
    var chatModelArr:[HWGameLiveChatModel]?{
        didSet {
            chatView.chatModelArr = chatModelArr
        }
    }
    var liveInfoModel:HWVoiceRoomInfoModel? {
        didSet {
            ownerHeadImageView.sd_setImage(with: URL(string: liveInfoModel?.face ?? ""), completed: nil)
            ownerName.text = liveInfoModel?.pet_name
            chatView.gameModel = liveInfoModel?.game
            zanLabel.text = liveInfoModel?.hot
            if liveInfoModel?.is_guanzhu == 0 {
                followButton.setTitle("订阅", for: .normal)
            }else {
                followButton.setTitle("已订阅", for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        ownerBgView.layoutIfNeeded()
//
//        let gradientColors = [UIColor.colorWithHexString(hexString: "#F7E8FF").cgColor,
//                              UIColor.colorWithHexString(hexString: "#FFFEFF").cgColor,
//
//        ]
//
//        //创建CAGradientLayer对象并设置参数
//        let gradientLayer = CAGradientLayer()
//        gradientLayer.colors = gradientColors
//
//        //设置渲染的起始结束位置（横向渐变）
//        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
//        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
//
//        //设置其CAGradientLayer对象的frame，并插入view的layer
//        gradientLayer.frame = ownerBgView.bounds
//        ownerBgView.layer.insertSublayer(gradientLayer, at: 0)
    }
}

extension HWGameLiveChatBaseView {
    private func initUI() -> Void {
        
        topBgView  = UIView()
        topBgView.backgroundColor = .white
        topBgView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#4A4A4A", alpha: 0.5).cgColor
        topBgView.layer.shadowOffset = CGSize(width: 0, height: -3)
        topBgView.layer.shadowOpacity = 1
        topBgView.layer.shadowRadius = 6
        addSubview(topBgView)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["聊天","排行"]
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#666666")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#F77105")
        segmentedDataSource.titleNormalFont = UIFont.systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 16*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedDataSource.itemSpacing = 25*widthScale
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#F77105")
        indicator.lineStyle = .normal
        indicator.indicatorHeight = 3*widthScale
        indicator.indicatorWidth = 20*widthScale
        indicator.verticalOffset = 0
        segmentedView.indicators = [indicator]
        
        topBgView.addSubview(self.segmentedView)
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        ownerBgView = UIView()
        ownerBgView.backgroundColor = .white
        ownerBgView.layer.cornerRadius = 39*widthScale/2
        ownerBgView.layer.masksToBounds = true
        topBgView.addSubview(ownerBgView)
        
        ownerHeadImageView = UIImageView()
        ownerHeadImageView.layer.cornerRadius = 34*widthScale/2
        ownerHeadImageView.layer.masksToBounds = true
        ownerBgView.addSubview(ownerHeadImageView)
        
        ownerName = HWLabel.createBoldLabel(title: "asdfasdf", color: "#333333", font: 12*widthScale)
        ownerBgView.addSubview(ownerName)
        
        heartImageView = UIImageView()
        heartImageView.image = UIImage(named: "liveZanSmall")
        ownerBgView.addSubview(heartImageView)
        
        zanLabel = HWLabel.createLabel(title: "1234", color: "#666666", font: 8*widthScale)
        ownerBgView.addSubview(zanLabel)
        
        followButton = UIButton(type: .custom)
        followButton.setTitle("订阅", for: .normal)
        followButton.backgroundColor = .colorWithHexString(hexString: "#FBDF41")
        followButton.layer.cornerRadius = 27*widthScale/2
        followButton.titleLabel?.font = .boldSystemFont(ofSize: 12*widthScale)
        followButton.setTitleColor(.colorWithHexString(hexString: "#505050"), for: .normal)
        followButton.addTarget(self, action: #selector(followButtonClick(_:)), for: .touchUpInside)
        topBgView.addSubview(followButton)
        
    }
    
    private func initFrame() -> Void {
        
        topBgView.snp.makeConstraints { make in
            make.left.top.right.equalTo(0)
            make.height.equalTo(45*widthScale)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.height.equalTo(40*widthScale)
            make.centerY.equalToSuperview()
            make.right.equalTo(-23*widthScale)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().offset(0)
            make.top.equalTo(topBgView.snp.bottom).offset(10*widthScale)
            make.bottom.equalTo(self.snp.bottom).offset(0)
        }
        
        ownerBgView.snp.makeConstraints { make in
            make.right.equalTo(-59*widthScale)
            make.top.equalTo(3*widthScale)
            make.bottom.equalTo(-3*widthScale)
        }
        
        ownerHeadImageView.snp.makeConstraints { make in
            make.height.width.equalTo(34*widthScale)
            make.left.equalTo(12*widthScale)
            make.top.equalTo(3*widthScale)
            
        }
        
        ownerName.snp.makeConstraints { make in
            make.left.equalTo(ownerHeadImageView.snp.right).offset(3*widthScale)
            make.top.equalTo(7*widthScale)
            make.right.equalTo(-12*widthScale)
        }
        
        heartImageView.snp.makeConstraints { make in
            make.left.equalTo(ownerName)
            make.bottom.equalTo(-6*widthScale)
            make.width.height.equalTo(7*widthScale)
        }

        zanLabel.snp.makeConstraints { make in
            make.left.equalTo(heartImageView.snp.right).offset(5*widthScale)
            make.centerY.equalTo(heartImageView)
        }
        
        followButton.snp.makeConstraints { make in
            make.right.equalTo(-7*widthScale)
            make.width.equalTo(52*widthScale)
            make.height.equalTo(27*widthScale)
            make.centerY.equalToSuperview()
        }
        
        
    }
}

extension HWGameLiveChatBaseView: JXSegmentedListContainerViewDataSource{
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0{
//            let chatView = HWGameLiveChatView()
//            hotListController.tagId = self.dataArr?[index].id ?? ""
            chatView.chatButtonClick = {
                self.chatButtonClick?()
            }
            chatView.managerButtonClick = {
                self.managerButtonClick?()
            }
            chatView.giftButtonClick = {
                self.giftButtonClick?()
            }
            chatView.downLoadButtonClick = {
                mod in
                self.downLoadButtonClick?(mod)
            }
            return chatView
        }else if index == 2{
            let friendsController = HWGameLiveFriendsListController()
            return friendsController
        }else {
            let rankListCenter = HWGameLiveRankListCenter()
//            listController.tagId = self.dataArr?[index].id ?? ""
            return rankListCenter
        }
    }
}

extension HWGameLiveChatBaseView {
    @objc func followButtonClick(_ button:UIButton) -> Void {
        self.followButtonClick?()
    }
}
